package com.yuyou.tas.gateway.filter;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;  import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.yuyou.tas.util.tool.LogUtil;

public class ErrorFilter extends ZuulFilter{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        Throwable throwable = ctx.getThrowable();
        ctx.set("error.status_code", HttpServletResponse.SC_OK);
        ctx.set("error.exception", throwable.getCause());
        LogUtil.error(logger, "gateway error"+throwable.getCause().getMessage());
        System.out.println(throwable.getCause().getMessage());
        throwable.printStackTrace();
          
        StringWriter sw = new StringWriter(); 
        throwable.printStackTrace(new PrintWriter(sw, true)); 
        String str = sw.toString();     
        //todo错误统一保存
        LogUtil.error(logger, str);
        
        return null;
	}
	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return 10;
	}

	@Override
	public String filterType() {
		 return "error";
	}

}
